<?php

class Products_model extends MY_Model {

    var $table_name = 'products';
    var $primary_key = 'id';

//    function get_by_id($id)
//    {
//        //$fields = 'products.*,picture.url';
//        $this->db->select('products.*,picture.url');
//        $this->db->where("{$this->table_name}.{$this->primary_key}",$id);
//        $this->db->join('folder_picture', "{$this->table_name}.images = folder_picture.folder_id AND folder_picture.order=1", 'left');
//    	$this->db->join('picture', "picture.id = folder_picture.picture_id", 'left');
//    	$this->db->group_by("{$this->table_name}.id");
//        $query = $this->db->get($this->table_name);
//        return $query->row_array();
//    }
    //Khi su dung nhieu images moi dung dem  ham nay
//    function get_list($start=null,$limit=null,$sort= '',$dir='DESC',$where = null)
//    {
//        $fields = 'products.*,picture.url';
//    	$this->db->select($fields);
//		$this->db->from($this->table_name);
//		if(!empty($where)) $this->db->where($where);
//    	$this->db->join('folder_picture', "{$this->table_name}.images = folder_picture.folder_id AND folder_picture.order=1", 'left');
//    	$this->db->join('picture', "picture.id = folder_picture.picture_id", 'left');
//
//    	if(empty($sort)) $this->db->order_by($this->primary_key, $dir);
//    	else $this->db->order_by($sort, $dir);
//		$this->db->order_by('folder_picture.order', 'ASC');
//		$this->db->group_by("{$this->table_name}.id");
//    	if(!empty($limit)) $this->db->limit($limit,$start);
//
//
//	    $query = $this->db->get();
//
//		return $query->result_array();
//
//    }
    function filterData(&$data) {        
        if (isset($data['onhome'])){
            if ($data['onhome'] == '') $data['onhome'] = 0;
        }
        if (isset($data['is_no_sale'])){
            if ($data['is_no_sale'] == '') $data['is_no_sale'] = 0;
        }
        if (isset($data['hot_item'])){
            if ($data['hot_item'] == '') $data['hot_item'] = 0;
        }
        if (isset($data['showtype'])){
            if ($data['showtype'] == '') $data['showtype'] = 0;
        }
       
        parent::filterData($data);
    }
    function updateOrder($oldorder, $order) {
        if (!is_numeric($oldorder) || !is_numeric($order))
            throw new Exception('Error in Product Model');
        if ($oldorder < $order) {
            $sql = "update {$this->table_name} set `order` = `order` - 1 where `order` <= $order and `order` > $oldorder";
        } else {
            $sql = "update {$this->table_name} set `order` = `order` + 1 where `order` < $oldorder and `order` >= $order";
        }
        return $this->db->query($sql);
    }

    function delete($id) {
        $data = $this->get_by_id($id);
        $sql = "update {$this->table_name} set `order` = `order` - 1 where `order` > {$data['order']}";
        $this->db->query($sql);
        return parent::delete($id);
    }
    
    function getInHomeProduct(){
        return $this->get_by_field(array('onhome'=>1));
    }
    
    function getProductNoSale(){
        return $this->get_by_field(array('is_no_sale'=>1));
    }
    

}

?>